package com.qf.shopping.controll;

import com.qf.shopping.pojo.System.User;
import com.qf.shopping.service.IArticleSericve;
import com.qf.shopping.ustils.UserUtils;
import com.qf.shopping.vo.article.ArticleConstom;
import com.qf.shopping.vo.article.ArticleSearch;
import com.qf.shopping.vo.article.ArticleVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 82024
 * @Date 2019/4/29 16:10
 * @Version 1.0
 * @Description
 **/
@Controller


@RequestMapping("/article")
@SessionAttributes("user")
public class ArticleControll {

    @Autowired
    private IArticleSericve articleSericve = null;

    /**
     * 资讯草稿箱的展示
     * @param model
     * @return 返回资讯草稿箱的页面
     */
    @GetMapping("/articles/audit")
    public String toarticle(Model model){
        ArrayList<ArticleVO> list = (ArrayList)articleSericve.findSomeArticle(null);
        model.addAttribute("lists",list);
        return  "/articles/audit/article";
    }

    /**
     * 加载添加资讯的页面
     * add页面还没写
     * @return 返回资讯添加页面
     */
    @GetMapping("/addarticle")
    public String toaddArticle(){
        return "articles/audit/articleadd";
    }

    /**
     * 添加资讯的页面的数据提交
     * @param articleConstom
     * @param session
     * @return  添加成功后跳转到资讯草稿箱
     */
    @PostMapping("/addarticle")
    public String addArtilce(ArticleConstom articleConstom, HttpSession session){
        User user = UserUtils.getUser(session);
        ArticleVO articleVO = new ArticleVO();
        articleVO.setArticleConstom(articleConstom);
        articleSericve.addArticle(articleVO, user);
        return "redirect:/article/articles/audit";
    }


    /**
     * 加载资讯修改页面
     * @return 返回资讯修改页面
     */
    @GetMapping("/updatearticle/{id}")
    public String toupdatearticle(@PathVariable("id") Long id, Model model){
        ArticleVO articleVO = articleSericve.findArticleById(id);
        model.addAttribute("article", articleVO.getArticleConstom());
        return "/articles/audit/articleupdate";
    }

    /**
     * 编辑资讯，数据提交
     * @param articleVO
     * @param session
     * @return  修改成功后跳转到资讯草稿箱
     */
    @PostMapping("/updatearticle")
    public String updateArticle(ArticleVO articleVO, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.editArticle(articleVO, user);
        return "redirect:/article/articles/audit";
    }


    /**
     * 加载资讯的审核列表页面
     * audit页面未写
     * @param model
     * @return  查询出所有待审核的资讯，返回审核资讯的列表页面
     */
    @GetMapping("/articles/articleaudit")
    public String toauditArticle(Model model){
        List<ArticleVO> list = articleSericve.auditArticle(null);
        model.addAttribute("lists", list);
        return "/articles/audit/articleaudit";
    }

    /**
     * 加载审核一条资讯的页面
     * audit页面未写
     * @param id
     * @param model
     * @return  根据ID找到资讯，放入域对象，返回审核页面
     */
    @GetMapping("/auditarticle/{id}")
    public String toauditArticle(@PathVariable("id")Long id, Model model){
        ArticleVO articleVO = articleSericve.findArticleById(id);
        model.addAttribute("article", articleVO);
        return "/articles/audit/articleauditone";
    }

    /**
     * 资讯审核通过
     * @param id
     * @param session
     * @return  通过审核以后，返回到资讯审核列表
     */
    @GetMapping("/passarticle/{id}")
    public String passArticle(@PathVariable Long id, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.passArticle(id, user);
        return "redirect:/article/articles/articleaudit";
    }

    /**
     * 驳回资讯
     * @param id
     * @param msg
     * @param session
     * @return  驳回资讯后，返回到资讯审核列表
     */
    @GetMapping("/disablearticle/{id}")
    public String disableArticle(@PathVariable("id") Long id, String msg, HttpSession session){
        User user = UserUtils.getUser(session);
        msg="审核不通过";
        articleSericve.disableArticle(id, msg, user);
        return "redirect:/article/articles/articleaudit";
    }


    /**
     * 展示单个资讯详情
     * show页面未完成
     * 页面上点击资讯的标题即可预览该资讯
     * 页面上显示资讯的名字，设置一个隐藏域用于存放该资讯的id
     * @param id
     * @return
     */
    @GetMapping("/show")
    public String showArticle(Long id, Model model){
        ArticleVO articleVO = articleSericve.findArticleById(id);
        model.addAttribute("article", articleVO);
        return "article/audit/show";
    }

    /**
     * 加载发布资讯的列表页面
     * publish页面未完成
     * @param model
     * @return  查找出所有待发布的资讯，返回发布资讯的列表页面
     */
    @GetMapping("/articles/publishlist")
    public String topublishArticle(Model model){
        List<ArticleVO> list = articleSericve.publishArticle(null);
        model.addAttribute("lists", list);
        return "articles/audit/articlepublish";
    }

    /**
     * 发布或者取消发布一条资讯
     * @param id
     * @param session
     * @return  根据ID查找到一条资讯并发布或者取消发布该资讯
     */
    @GetMapping("/publish/{id}")
    public String publishArticle(@PathVariable("id")Long id, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.publishArticle(id, user);
        return "redirect:/article/articles/publishlist";
    }

    /**
    * 资讯的推荐或者取消推荐
    * @Param
    * @return
    */
    @GetMapping("/top/{id}")
    public String totopArticle(@PathVariable("id") Long id, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.topArticle(id, user);
        return "redirect:/article/articles/publishlist";
    }


    /**
    * 加载资讯的展示列表页面
     * list页面未完成
    * @Param
    * @return
    */
    @GetMapping("/articles/articlelist")
    public String toarticleList(Model model){
        List<ArticleVO> list = articleSericve.findAllArticle(null);
        model.addAttribute("lists", list);
        for (ArticleVO vo : list) {
            System.out.println(vo);
        }
        return "articles/audit/articlelist";
    }

    /**
    * 资讯列表的搜索功能
    * @Param
    * @return
    */
    @PostMapping("/search")
    public String searchArticle(ArticleSearch articleSearch, Model model){
        List<ArticleVO> list = articleSericve.findAllArticle(articleSearch);
        model.addAttribute("lists", list);
        return "article/audit/list";
    }


    /**
    * 根据ID删除资讯
    * @Param
    * @return
    */
    @GetMapping("/deletearticle/{id}")
    public String deleteArticle(@PathVariable("id") Long id, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.deleteArticleById(id, user);
        return "redirect:/article/articles/audit";
    }


    @GetMapping("/submitarticle/{id}")
    public String submitArticle(@PathVariable("id") Long id, HttpSession session){
        User user = UserUtils.getUser(session);
        articleSericve.submitArticle(id, user);
        return "redirect:/article/articles/audit";
    }













    //车型报告
    @GetMapping("/report")
    public String toreport(Model model){
        return "/articles/audit/report";
    }

    @GetMapping("/updatereport")
    public String  toupdatereport(){
        return "/articles/audit/reportupdate";
    }




    //网络搜索
    @GetMapping("/search")
    public String tosearch(Model model){
        return "/articles/audit/search";
    }

    @GetMapping("/updatesearch")
    public String toupdatesearch(){
        return "/articles/audit/searchupdate";
    }










//    /**
//     * 资讯草稿箱的搜索功能
//     * @param title
//     * @param startTime
//     * @param endTime
//     * @param model
//     * @return
//     */
//    @GetMapping("/drafts/search")
//    public String articleDraftsSearch(String title, Date startTime, Date endTime, Model model){
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("title", title);
//        map.put("startTime", TimeUtil.getTime(startTime));
//        map.put("endTime", TimeUtil.getTime(endTime));
//        List<ArticleVO> list = articleSericve.findSomeArticle(map);
//        model.addAttribute("data", list);
//        return "";
//    }






}
